package com.smartHome.controller;

import com.smartHome.pojo.entity.Result;
import com.smartHome.utils.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {
    @Autowired
    private JavaMailSender sender; // 引入Spring Mail依赖后，会自动装配到IOC容器。用来发送邮件
    @GetMapping("/getCode/{email}")
    public Result test1(@PathVariable String email){
        String code = VerificationCodeUtil.generateVerificationCode();// 生成随机验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("【测试验证码】验证消息"); // 发送邮件的标题
        message.setText("登录操作，验证码："+ code + "，切勿将验证码泄露给他人，本条验证码有效期2分钟。"); // 发送邮件的内容
        message.setTo(email); // 指定要接收邮件的用户邮箱账号
        message.setFrom("1992515303@qq.com"); // 发送邮件的邮箱账号，注意一定要和配置文件中的一致！
        sender.send(message);// 调用send方法发送邮件即可
        return Result.success(code);
    }
}
